# _*_ coding utf-8 _*_
# FILENAME：.py
# DESCRIPTION: 
# AUTH: 
# DATE: 2022/6/110:56 上午

from PyQt5.QtWidgets import *

from models.versionModel import VersionModel

class VersionForm(QDialog):

    __id = 0  # 版本编号
    '''
    id 版本编号
    '''
    def __init__(self, parent=None, id=0, name=None):
        super(VersionForm, self).__init__(parent)
        self.init_ui(parent)
        self.__id = id
        title = "新增版本"
        if id > 0:
            title = "编辑版本"
            self.ver_text.setText(name)
        self.setWindowTitle(title)

    def init_ui(self, parent):
        '''水平布局'''
        hbox = QHBoxLayout()
        self.save_btn = QPushButton()
        self.save_btn.setText('保存')
        self.save_btn.clicked.connect(lambda: self.save_btn_click(parent))

        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.cancel_btn.clicked.connect(self.cancel_btn_click)

        hbox.addWidget(self.save_btn)
        hbox.addWidget(self.cancel_btn)

        '''表单布局'''
        fbox = QFormLayout()
        # self.fac_lab = QLabel()
        # self.fac_lab.setText('工厂：')
        # self.fac_combox = QComboBox()
        # self.fac_combox.setPlaceholderText('请输入序号')
        # self.fac_combox.addItems(["工厂一|1", "工厂二|2", "工厂三|3", "工厂四|4"])
        # self.fac_combox.setCurrentIndex(0)

        self.ver_lab = QLabel()
        self.ver_lab.setText('版本：')
        self.ver_text = QLineEdit()
        self.ver_text.setPlaceholderText('请输入版本')

        # fbox.addRow(self.fac_lab, self.fac_combox)
        fbox.addRow(self.ver_lab, self.ver_text)

        vbox = QVBoxLayout()
        vbox.addLayout(fbox)
        vbox.addLayout(hbox)

        self.setLayout(vbox)

    def save_btn_click(self, parent):
        if self.ver_text.text().strip() != '':
            model = VersionModel()
            # 判断是更新或者新增
            if self.__id > 0:
                model.id = self.__id
                param = {'name': self.ver_text.text()}
                update(model, param)
            else:
                model.name = self.ver_text.text()
                add(model)
            self.close()
            # 刷新列表
            self.parent().query_data_list()

    def cancel_btn_click(self):
        self.close()

    @staticmethod
    def get_dialog(parent=None, id=0, name=None):
        dialog = VersionForm(parent, id, name)
        return dialog.exec()

